﻿using HuaJuanAudioNevolDown;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Forms;

public class Service
{
    private static readonly Service _instance = new Service();
    private readonly HttpClient _httpClient = new HttpClient();

    public static Service Instance
    {
        get
        {
            return _instance;
        }
    }

    public string IDMPath
    {
        get
        {
            var path = ConfigurationManager.AppSettings["IDMPath"];
            if (string.IsNullOrEmpty(path))
            {
                return "C:\\Program Files (x86)\\Internet Download Manager\\IDMan.exe";
            }
            return ConfigurationManager.AppSettings["IDMPath"];
        }
        set
        {
            var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            configFile.AppSettings.Settings["IDMPath"].Value = value;
            configFile.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
        }
    }

    public string DownPath
    {
        get
        {
            var path = ConfigurationManager.AppSettings["DownPath"];
            if (string.IsNullOrEmpty(path))
            {
                return Application.StartupPath;
            }
            return ConfigurationManager.AppSettings["DownPath"];
        }
        set
        {
            var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            configFile.AppSettings.Settings["DownPath"].Value = value;
            configFile.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
        }
    }

    public async Task<VersionInfo> GetRemoteVersionAsync(string url)
    {
        VersionInfo versionInfo = null;
        // 发送 GET 请求
        var response = await _httpClient.GetAsync(url);
        if (response.IsSuccessStatusCode)
        {
            // 读取响应内容
            var jsonResponse = await response.Content.ReadAsStringAsync();
            // 反序列化 JSON 响应
            versionInfo = JsonSerializer.Deserialize<VersionInfo>(jsonResponse);
        }
        return versionInfo;
    }

    public VersionInfo GetLocalVersion(string versionFile)
    {
        string jsonString = File.ReadAllText(versionFile); // 读取文件内容
        VersionInfo versionInfo = JsonSerializer.Deserialize<VersionInfo>(jsonString);
        return versionInfo;
    }

    public Dictionary<string, IExtractor> GetAllExtractors()
    {
        IExtractor extra = null;
        Dictionary<string, IExtractor> extractors = new Dictionary<string, IExtractor>();

        extra = new ExtraYuetingbaCn();
        extractors.Add(extra.Site, extra);

        extra = new Extra70tsCom();
        extractors.Add(extra.Site, extra);
        return extractors;
    }
}